package com.aco.fatureapp.activity;

import javax.inject.Inject;

import roboguice.inject.ContentView;
import roboguice.inject.InjectView;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.aco.fatureapp.R;
import com.aco.fatureapp.model.Imovel;
import com.aco.fatureapp.service.SrvBilling;
import com.aco.fatureapp.utilitary.Utils;

@ContentView(R.layout.activity_metering_erase)
public class EraseMeteringActivity extends BaseActivity {

	@InjectView(R.id.txtCodigoBarra)
	private EditText mTxtCodigoBarra;

	@InjectView(R.id.btnOKE)
	private Button mBtnOk;

	@InjectView(R.id.btnCancelE)
	private Button mBtnCancel;

	public static String RETURN = "EraseMeteringActivity.return";

	/*
	 * Info
	 */
	private Long mInscricao;

	@Inject
	private SrvBilling mSrvBilling;

	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setFinishOnTouchOutside(false);

		mInscricao = getIntent().getExtras().getLong(
				Imovel.getTableName(Imovel.class));

		/**
		 * Registrando Eventos
		 */
		mBtnOk.setOnClickListener(onEraseMetering());

		mBtnCancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(final View v) {
				finish();
			}
		});
	}

	private OnClickListener onEraseMetering() {
		return new OnClickListener() {

			@Override
			public void onClick(final View v) {
				String codigoBarra = mTxtCodigoBarra.getText().toString();

				if (codigoBarra == null || codigoBarra.trim().length() == 0) {
					Utils.alert(EraseMeteringActivity.this,
							"Informe o código de barras"
									+ " ou inscrição para corrigir a leitura.",
							null);
					return;
				}

				String codBarraBanco = mSrvBilling.getCodigoBarra(mInscricao);

				if (codBarraBanco == null) {
					codBarraBanco = mInscricao.toString();
				}

				if (!codigoBarra.equalsIgnoreCase(codBarraBanco)) {
					Utils.alert(EraseMeteringActivity.this, "Código de barras"
							+ " ou inscrição informado para correção"
							+ " estão inválidos, informe novamente.", null);
					return;
				}

				mSrvBilling.refaturar(mInscricao, usuarioLogado.getId());

				Utils.alert(EraseMeteringActivity.this,
						"Conta excluída, liberado para correção da leitura.",
						new Dialog.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {

								setResult(RESULT_OK,
										getIntent().putExtra(RETURN, 100));

								finish();
							}
						});
			}
		};
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onBackPressed()
	 */
	@Override
	public void onBackPressed() {
	}

}
